

#delimit;
set more off;
  quietly log;
  local logon = r(status);
  if "`logon'" == "on" {; log close; };
set mat 800;

/*	*****************************************************/
/*     	File Name:	mainanalysis.do						*/
/*     	Date:   	Sept 13 2018						*/
/*      Author: 	Zachary D. Greene					*/
/*      Purpose:	Predicts vote choice from ENMI		*/
/* 		Packages: estout.pkg							*/
/*      Output File:	 								*/
/*														*/
/*	Directory:											*/



cd "~\Replication files";
	
use "ENMI - replication.dta", clear;

#delimit;
		

/* Table 2, Model 3	*/	
	#delimit;	
xtreg pvote
	c.ensh_mi##i.inc_cab_l pvote_l
	c.GDPgrowth##i.inc_cab_l
	
	c.moderate##i.inc_cab_l

 majelect  
  , fe robust  ;
  
estimates store full_fe;	

#delimit;
corr rile ensh_mi if e(sample);
corr rile ensh_mi if e(sample) & inc_cab_l==1;
corr rile ensh_mi if e(sample) & inc_cab_l==0;


testnl _b[c.ensh_mi#1.inc_cab_l] + _b[c.ensh_mi]
== _b[c.ensh_mi] ;

		 
*egen pcount=group(party) if e(sample);
	sum pcount; /* The number of parties in the sample	*/

*egen etotal=group(edate_numeric) if e(sample);
	sum etotal; /* The number of elections in the sample	*/
	

	#delimit;
estpost summarize
 pvote  
ensh_mi 
 moderate
 GDPgrowth
inc_cab_l 
 majelect
if e(sample);

/* Generates Table 1	*/
/* To creat the exact tables from the analysis, */
/* you'll need to install the estout package	*/
*net install estout;

#delimit;
esttab . using "Table1-samplestatistics.rtf" ,
	label replace cells("mean(fmt(%9.3f)) sd(fmt(%9.3g)) 
	min(fmt(%9.3g)) max(fmt(%9.3g)) count(fmt(%9.0g))"   ) compress 
	 coeflabels( pvote "Percentage Vote"
ensh_mi "ENMI" 
1.inc_cab_l "Incumbent (0,1)"
GDPgrowth "GDP Growth Rate" 
moderate "Moderated Position (0,1)"
majelect "Majoritarian Election (0,1)"
);

	

/* Table 2, Model 1	*/	
xtreg pvote
	c.ensh_mi c.GDPgrowth pvote_l
	moderate 1.nparty
		 majelect federal directpres 
		
		if inc_cab_l==0 , fe robust ;
	
	testnl _b[c.ensh_mi] == 0 ;
	
		estimates store onlyopposition_fe;

/* Table 2, Model 2	*/
xtreg pvote
	1.inc_cab_l#c.ensh_mi pvote_l c.GDPgrowth
	1.inc_cab_l#c.moderate 
	 majelect 
  if inc_cab_l==1 , fe robust;
		
testnl _b[c.ensh_mi#1.inc_cab_l] == 0 ;

		estimates store onlyincumbents_fe;
		
						#delimit;


	
/* Creates Table 2 in the Main Text	*/

#delimit;
 esttab onlyopposition_fe onlyincumbents_fe  full_fe  
 using "Table2-FEOLS.rtf", replace 
		b (%10.3f) se scalars("aic AIC" "bic BIC" "rmse Root Mean Squared Error" "chi2 \u0935?{\super 2}" "ll Log-Likelihood" ) 
		obslast star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
		label mtitles (  "Opposition" "Incumbents" "Full") nogaps
title (\label{})
		
		
order(ensh_mi 
1.inc_cab_l#c.ensh_mi 
moderate 
1.inc_cab_l#c.moderate 
GDPgrowth
1.inc_cab_l#c.GDPgrowth 
1.inc_cab_l 

majelect pvote_l _cons ) 

keep(ensh_mi 1.inc_cab_l 
1.inc_cab_l#c.ensh_mi GDPgrowth 
1.inc_cab_l#c.GDPgrowth 1.inc_cab_l#c.moderate 
moderate majelect 
pvote_l _cons )  

coeflabels(
ensh_mi "ENMI"  
1.inc_cab_l "Incumbent"
1.inc_cab_l#c.ensh_mi "Incumbent X ENMI"
GDPgrowth "GDP Growth Rate" 
1.inc_cab_l#c.GDPgrowth "Incumbent X GDP Growth Rate"
1.inc_cab_l#c.moderate "Incumbent X Moderated Position"
moderate "Moderated Position"
majelect "Majoritarian Election"
pvote_l "% Vote{\sub t-1}"
_cons "Constant");


/********************/
/* Create Figure 1	*/
/********************/

#delimit;
twoway connected ensh_mi year if  party==51620 & year>1960,  lpattern("..") 
	scheme(s1mono) msymbol(x) sort yaxis(1) ytitle("ENMI") ||
	connected pvote year if  party==51620 & year>1960,  msymbol(T) lpattern("l")
	yaxis(2) ytitle("% Vote", axis(2))
	title("Conservative Party") sort legend(order(1 "ENMI" 2 "% Vote"))
	saving(figures\conservatives.gph, replace);
	
#delimit;
	twoway connected ensh_mi year if  party==51320 & year>1960,  lpattern("..") 
	scheme(s1mono) msymbol(x) sort yaxis(1) ytitle("ENMI")||
	connected pvote year if  party==51320& year>1960,  msymbol(T) lpattern("l")
	yaxis(2) ytitle("% Vote", axis(2))
	title("Labour Party") sort legend(order(1 "ENMI" 2 "% Vote"))
	saving(figures\labour.gph, replace);
	

#delimit;
graph combine figures\conservatives.gph figures\labour.gph, scheme(s1mono) cols(2) ycommon;
	
	graph export figures\Figure1.pdf, replace;
	
/********************************************/
/* Estimate the Predicted Effect of ENMI	*/
/* from Model 3 and creat Figure 2.			*/
/********************************************/	
		
	 #delimit;
xtreg pvote
c.ensh_mi##i.inc_cab_l pvote_l
c.GDPgrowth##i.inc_cab_l
i.moderate##i.inc_cab_l
majelect  
, fe robust  ;

  
/*Wald tests of joint significance.*/
testnl _b[c.ensh_mi#1.inc_cab_l] 
  + _b[c.ensh_mi]==0;

testnl _b[c.ensh_mi#1.inc_cab_l] 
+ _b[c.ensh_mi]== 
_b[c.ensh_mi]==0;

testnl _b[c.ensh_mi]==0 ;


		mat list e(b);
		mat def b = e(b);
		mat list e(V);
		mat def V = e(V);
		
		keep if e(sample);

		collapse (mean)  pvote
	 pvote_l
	GDPgrowth mean_ensh_mi=ensh_mi
		(sd) sd_ensh_mi=ensh_mi
		(max) max_ensh_mi=ensh_mi
		(min) min_ensh_mi=ensh_mi;
		
		expand 100;
		
		gen enmi=  min_ensh_mi +  (( max_ensh_mi)-( min_ensh_mi))*_n/_N;
		
save sims\enmi.dta, emptyok replace;
	 /*save dataset	*/


program drop _all;

	program define enmi,;
	
		syntax,;
	use   sims\enmi.dta, clear;
	
	drawnorm b1-b17, means(b) cov(V);
		
		gen enmi_gov= b1*enmi + b3*1 +b5*1*enmi + b6*pvote_l + b7*GDPgrowth +b9*1*GDPgrowth
			+ b11*0 + b15*1*0 + b16*0 + b17 ;
		gen enmi_opp= b1*enmi + b3*0 +b5*0*enmi + b6*pvote_l + b7*GDPgrowth +b9*0*GDPgrowth
			+ b11*0 + b15*0*0 + b16*0 + b17 ;
		
		gen sd_enmi_gov= (b1*(mean_ensh_mi + sd_ensh_mi) + b3*1 +b5*1*(mean_ensh_mi+sd_ensh_mi) + b6*pvote_l + b7*GDPgrowth +b9*1*GDPgrowth
			+ b11*0 + b15*1*0 + b16*0 + b17) - (b1*(mean_ensh_mi-sd_ensh_mi) + b3*1 +b5*1*(mean_ensh_mi-sd_ensh_mi) + b6*pvote_l + b7*GDPgrowth +b9*1*GDPgrowth
			+ b11*0 + b15*1*0 + b16*0 + b17 );

		
	append using sims\results.dta;
	save sims\results.dta, replace;
	
	
	
end;

clear;

	save sims\results.dta, emptyok replace; /*save empty dataset for results*/

/*run loop to perform program at multiple differences*/

set more off;
	use sims\enmi.dta, clear	;

	
		simulate, reps(1000) : enmi,   ;




/***The loop below opens up the dataset of results and performs _pctile across the range of time for each value of delta, producing the 95% CIs***/
#delimit ;

use sims\results.dta, clear;

foreach var of varlist enmi_gov enmi_opp sd_enmi_gov {;
/*D_p05_pct*/
statsby `var'_lo=r(r1) `var'_median=r(r2) `var'_hi=r(r3), by(enmi)
saving(sims\CIs_`var'.dta, replace): _pctile `var', p(2.5, 50, 97.5);
}
;

	
	/*define local containing names of all but first variable*/
	#delimit;
	set more off;
use sims\CIs_enmi_gov.dta, clear; 	/*open dataset of CIs for first variable*/
	append using sims\using CIs_enmi_opp.dta ;
	append using sims\using CIs_sd_enmi_gov.dta ;
	append using sims\using ENMI - Replication.dta;
	
	
	
	gen xtick=13.1;
	gen xtick2=13.05;


#delimit;
twoway  lowess  enmi_gov_median enmi if enmi<40, sort scheme(s1mono) lcolor(cranberry) saving(, replace) 
		||  lowess enmi_gov_lo  enmi if enmi<40, lpattern(".") lcolor(red) lwidth(medium)
		|| lowess enmi_gov_hi  enmi if enmi<40, lpattern(".")  lcolor(red) lwidth(medium)
		|| lowess  enmi_opp_median enmi if enmi<40, sort  lcolor(blue*.3) lpattern(-.)
		||  lowess enmi_opp_lo  enmi if enmi<40, lpattern("-") lcolor(dknavy*.3)
		|| lowess enmi_opp_hi  enmi if enmi<40, lpattern("-")  lcolor(dknavy*.3)
		||	spike xtick ensh_mi  if ensh_mi<40 & inc_cab_l==1, base(13)
		||	spike xtick2 ensh_mi  if ensh_mi<40 & inc_cab_l==0, base(13) lcolor(gs6)

			  title("Predicted difference in % Vote" "from ENMI")
			  ytitle("Difference in % Vote") 
			  xtitle("ENMI") xtick() xlabel() 
			legend(order(1 "Government" 4 "Opposition"))  ;

	graph export "figures\Figure2.pdf", replace;
			

			
	/* Estimate the percentage change used in the text	*/
sum sd_enmi_gov*;
	
	egen min_ensh_mi= min(ensh_mi);
	egen max_ensh_mi= max(ensh_mi);
	egen mean_sd_enmi_gov_median= mean(sd_enmi_gov_median);
	
	gen per_change= mean_sd_enmi_gov_median/ (abs(min_ensh_mi - max_ensh_mi));
sum per_change;

